package com.bawei.library_mvi.ui

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import com.bawei.library_mvi.R
import com.bawei.library_mvi.loadsirr.EmptyCallBack
import com.bawei.library_mvi.loadsirr.ErrorCallback
import com.bawei.library_mvi.loadsirr.LottieLoadingCallback
import com.kingja.loadsir.callback.Callback
import com.kingja.loadsir.core.LoadService
import com.kingja.loadsir.core.LoadSir


abstract class BaseFragment<VDB: ViewDataBinding>  : Fragment(),IBaseView {
    protected lateinit var viewDataBinding:VDB
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View? {
        viewDataBinding = DataBindingUtil.inflate(inflater,getLayout(),container,false)
        initEnv()
        initView()
        initEvent()
        return viewDataBinding.root
    }


    open fun initEnv(){} //初始化环境：viewmodel
    abstract fun getLayout():Int //返回布局id
    open fun initView(){} //初始化view
    open fun initData(){}//初始化数据
    open fun initEvent(){}//初始化 事件监听



    //吐司方法
    fun showMsg(msg:String){
        //判断是否为主线程
        if(Looper.getMainLooper().thread == Thread.currentThread()){
            Toast.makeText(context,msg, Toast.LENGTH_SHORT).show()
        }else{
            //子线程切换到主线程
            handler.post{
                Toast.makeText(context,msg, Toast.LENGTH_SHORT).show()
            }
        }
    }
    private val  handler: Handler = Handler(Looper.getMainLooper())
    //解决handler的内存泄漏问题
    override fun onDestroy() {
        super.onDestroy()
        handler.removeCallbacksAndMessages(null)
    }
    private lateinit var mLoadService: LoadService<*>
    private var isShowedContent:Boolean = false //是否显示内容
    //设置指定View
    fun setLoadSir(view:View){
        mLoadService = LoadSir.getDefault()
            .register(view) { onRetryBtnClick() }
    }

    //显示内容
    override fun showContent() {
        if (null != mLoadService) {
            isShowedContent = true
            mLoadService.showSuccess()//成功
        }
    }
    //加载中
    override fun showLoading() {
        if (null != mLoadService) {
            mLoadService.showCallback(LottieLoadingCallback::class.java)//加载中
        }
    }
    //空
    override fun showEmpty() {
        if (null != mLoadService) {
            mLoadService.showCallback(EmptyCallBack::class.java)
        }
    }
    //失败
    //第一种情况：有内容-》上拉下拉-》失败-》显示之前的内容
    //第二种情况：失败-》显示失败页面
    override fun showFailure(message: String?) {
        if (null != mLoadService) {
            if (!isShowedContent) {//之前是否显示成功过
                mLoadService.showCallback(ErrorCallback::class.java)
            } else {
                message?.let { showMsg(it) }
            }
        }
    }

    /**
     * 失败重试,加载事件
     */
     open fun onRetryBtnClick(){

     }



}